var JXXC = {

}

JXXC.mode = {
	user: {
		//myzii  admin123
		login: function(param) {

			myurl = JXXC.utils.url.get('Login', 'login');
			mui.ajax(myurl, {
				data: param,
				type: 'post',
				success: function(datas) {
					if (datas.s > 0) {

						JXXC.utils.cookie.addcookie([{
							name: 'uid',
							value: datas.d.userid
						}, {
							name: 'name',
							value: datas.d.username
						}, {
							name: 'avator',
							value: datas.d.photo
						}], 1); //x个小时

						//界面变化
						plus.webview.hide('login.html');
						JXXC.utils.isLogin();
					} else {
						mui.alert('用户名或密码错误');
					}
				}
			});
		},
		logout: function() {
			JXXC.utils.cookie.delCookie('uid');	
		}
	}
}
JXXC.action = {

	index_init: function() {

		var subPages = ['new.html', 'xueche.html', 'faxian.html', 'setting.html'];
		var subPages_style = {
			top: '45px',
			bottom: '50px'
		}
		mui.plusReady(function() {
			var _self = plus.webview.currentWebview();
			for (var i = 0; i < subPages.length; i++) {
				var sub = plus.webview.create(subPages[i], subPages[i], subPages_style);
				if (i > 0) {
					sub.hide();
				}
				_self.append(sub);
			}
		});
		var activeTab = subPages[0];
		mui('.tabBottm').on('tap', 'a', function(e) {
			var targetTab = this.getAttribute('href');
			if (targetTab == activeTab) {
				return;
			}
			plus.webview.show(targetTab);
			plus.webview.hide(activeTab);
			activeTab = targetTab;
		});
	},

	'news': {
		/*首页banner广告位*/
		index_banner: function(callback) {
			myurl = JXXC.utils.url.get('Index', 'infoList');
			mui.ajax(myurl, {
				data: {
					type: 2
				},
				timeout: 10000,
				type: 'get',
				success: function(datas) {
					callback(datas);
				},

			});
		},

		//类型id  1:最新动态 2:广告位 3:系统公告 4:教练风采  默认取最新动态

		/*首页列表-最新动态*/
		index_news: function(callback) {

			myurl = JXXC.utils.url.get('Index', 'infoList')

			mui.ajax(myurl, {
				data: {
					type: 1
				},
				type: 'get',
				success: function(datas) {
					callback(datas);
				}
			});
		},
		/*首页列表-系统公告*/
		index_system: function(callback) {

			myurl = JXXC.utils.url.get('Index', 'infoList')
			mui.ajax(myurl, {
				data: {
					type: 3
				},
				type: 'get',
				success: function(datas) {
					callback(datas);
				}
			});
		}
	},

	'xueche': {
		/*学车列表-精彩学车*/
		xc_study: function(callback) {
			myurl = JXXC.utils.url.get('Index', 'infoList');
			mui.ajax(myurl, {
				data: {
					type: 1
				},
				type: 'get',
				timeout: 10000,
				success: function(datas) {
					callback(datas);
				}
			});
		},
		/*学车列表-教练风采*/

		xc_jl: function(callback) {
			myurl = JXXC.utils.url.get('Index', 'infoList');
			mui.ajax(myurl, {
				data: {
					type: 4
				},
				type: 'get',
				timeout: 10000,
				success: function(datas) {
					callback(datas);
				}
			});
		},
		/*学车列表-教学场地*/
		xc_cd: function(callback) {
			myurl = JXXC.utils.url.get('Index', 'infoList');
			mui.ajax(myurl, {
				data: {
					type: 1
				},
				type: 'get',
				timeout: 10000,
				success: function(datas) {
					callback(datas);
				}
			});
		}

	},

	'display': {
		showDis: function(pid, callback) {
			myurl = JXXC.utils.url.get('Index', 'getInfoById');
			$.ajax({
				type: "get",
				url: myurl,
				data: {
					id: pid
				},
				timeout: 10000,
				success: function(datas) {
					callback(datas);
				}
			});
		}
	}

}

JXXC.utils = {

	url: {
		param_name: "/index.php?",
		//获取url
		get: function(controllor, action, params) {
			var myurl;
			controllor = controllor || '';
			action = action || '';
			params = params || {};
			myurl = ApiUrl + this.param_name + 'c=' + controllor + '&a=' + action + '&debug=1';
			return myurl;
		}
	},
	//验证是否登录
	isLogin: function() {

		var uid = JXXC.utils.cookie.getcookie('uid');
		nickname = JXXC.utils.cookie.getcookie('name');
		avator = JXXC.utils.cookie.getcookie('avator');

		var Odata = {};
		if (uid > 0) {
			Odata = {
				id: uid,
				name: nickname,
				photo: avator
			}
			var setting = plus.webview.getWebviewById("setting.html");
			mui.fire(setting, 'refresh', Odata);
			return true;
		}
		return false;
	},

	cookie: {

		addcookie: function(param, expireHours) {
			for (var o in param) {
				var cookieString = param[o].name + "=" + escape(param[o].value) + "; path=/";
				if (expireHours > 0) {
					var date = new Date();
					date.setTime(date.getTime + expireHours * 60 * 60);
					cookieString = cookieString + "; expire=" + date.toGMTString();
				}
				document.cookie = cookieString;
			}
		},
		getcookie: function(name) {
			var strcookie = document.cookie;
			var arrcookie = strcookie.split("; ");
			for (var i = 0; i < arrcookie.length; i++) {
				//console.log(arrcookie[i]);
				var arr = arrcookie[i].split("=");
				if (arr[0] == name) return arr[1];
			}
			return "";

		},
		delCookie: function(name) {
			var exp = new Date();
			exp.setTime(exp.getTime() - 1);
			var cval = JXXC.utils.cookie.getcookie(name);
			if (cval != null) document.cookie = name + "=" + cval + "; path=/;expires=" + exp.toGMTString();

		},
	},

	/**
	 * id:		 	新闻id
	 * target:		加载到那个页面
	 */
	loadDisplay: {
		'loadDis': function(Pid, target) {
			/**
			 * target如果不填写，默认到display.html
			 */
			if (target == null) {
				mui.preload({
					url: "display.html",
					id: "display"
				});
			} else {
				mui.preload({
					url: target,
					id: "display"
				});
			}
			var dis = plus.webview.getWebviewById("display");

			mui.fire(dis, 'loadDetail', {
				p_id: Pid
			});

		}
	}

}